package com.mymobile.showcase.mobile;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ApplicationScoped;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;

import com.hmsproject.db.util.HMSDBUtil;

@ManagedBean
@SessionScoped
public class SignInUserView {
	
	private boolean userLoggedIn;
	private String redirectURL = "/search/search.xhtml";
	
	public String getEmailAddress() {
		return emailAddress;
	}

	public void setEmailAddress(String emailAddress) {
		this.emailAddress = emailAddress;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	private String emailAddress;
	private String password;	
	
	public boolean isUserLoggedIn(){
		return this.userLoggedIn;
	}
	
	public String logout(){
		this.emailAddress = null;
		this.password = null;
		this.userLoggedIn = false;
		return "/index.xhtml?faces-redirect=true";
	}
	
	public String signInUser()
	{
		boolean loggedin = HMSDBUtil.getInstance().signInUser(this);
		System.out.println("User logged in " + loggedin + "    " + this.getEmailAddress());
		
		if(!loggedin){
			FacesContext.getCurrentInstance().addMessage(null, 
			        new FacesMessage(FacesMessage.SEVERITY_ERROR, "Incorrect username/password, please check.", null));	
			return null;
		}
		this.userLoggedIn = true;
		return this.redirectURL+"?faces-redirect=true";
	}
	

	public String signInUserMain(){
		return this.redirectToSignPage("/search/search.xhtml");
	}
	
	
	public String redirectToSignPage(String redirectPage){
		 this.setRedirectURL(redirectPage);
		 return "/user/signIn.xhtml?faces-redirect=true";
	}
	
	
	public String getRedirectURL() {
		return redirectURL;
	}

	public void setRedirectURL(String redirectURL) {
		this.redirectURL = redirectURL;
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		SignInUserView signInUserView = new SignInUserView();
		signInUserView.setEmailAddress("satsrinister@gmail.com");
		signInUserView.setPassword("satsrini");
		signInUserView.signInUser();

	}

}
